package com.moyq5.permit.center.feign.server.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.converter.json.MappingJacksonValue;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.moyq5.mvc.framework.general.Body;
import com.moyq5.mvc.framework.general.Page;
import com.moyq5.mvc.framework.general.Paging;
import com.moyq5.mvc.framework.general.Sorting;
import com.moyq5.mvc.framework.jackson.result.ListResult;
import com.moyq5.mvc.framework.jackson.result.ObjectResult;
import com.moyq5.mvc.framework.jackson.result.PageResult;
import com.moyq5.mvc.framework.jackson.result.StatusResult;
import com.moyq5.mvc.framework.utils.EntityUtils;
import com.moyq5.mvc.framework.utils.StringUtils;
import com.moyq5.permit.center.bean.entity.SysAdmin;
import com.moyq5.permit.center.bean.search.SysAdmin4Search;
import com.moyq5.permit.center.core.MyUtil;
import com.moyq5.permit.center.core.service.SysAdminService;
import com.moyq5.permit.center.feign.server.BaseController;
import com.moyq5.permit.center.feign.server.jackson.filter.SysAdminFilter;


/**
 * @author Moyq5’s Tools
 * @since 2019-07-12 16:16:27
 */
@RestController
@RequestMapping("/admin")
public class SysAdminController extends BaseController {
	
	private static Logger log = LoggerFactory.getLogger(SysAdminController.class);
	
	@Autowired
	private SysAdminService adminService;
	
	/**
	 * 根据ID获取对象
	 * @author Moyq5’s Tools
	 * @param adminId
	 * @param filter
	 * @return
	 */
	@PostMapping("/oneById")
	@ResponseBody
	public MappingJacksonValue oneById(@RequestBody Long adminId, String filter) {
		log.debug(">>>>oneById: adminId={},filter={}", adminId, filter);
		SysAdmin admin = adminService.one(adminId);
		ObjectResult<SysAdmin> objRst = new ObjectResult<SysAdmin>();
		objRst.setData(admin);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(SysAdminFilter.get(filter));
		return value;
	}
	
	/**
	 * 查询记录数
	 * @author Moyq5’s Tools
	 * @param admin4s
	 * @return
	 */
	@PostMapping("/count")
	@ResponseBody
	public ObjectResult<Long> count(@RequestBody Body<SysAdmin4Search, String> admin4s) {
		log.debug(">>>>count: ");
		Long count = adminService.count(admin4s.getEntity(), admin4s.getCustom());
		ObjectResult<Long> objRst = new ObjectResult<Long>();
		objRst.setData(count);
		objRst.setOk();
		return objRst;
	}
	
	/**
	 * 查询单个对象
	 * @author Moyq5’s Tools
	 * @param admin4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/one")
	@ResponseBody
	public MappingJacksonValue one(@RequestBody Body<SysAdmin4Search, Sorting> admin4s, String filter) {
		log.debug(">>>>one: filter={}", filter);
		SysAdmin admin = adminService.one(admin4s.getEntity(), admin4s.getCustom());
		ObjectResult<SysAdmin> objRst = new ObjectResult<SysAdmin>();
		objRst.setData(admin);
		objRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(objRst);
		value.setFilters(SysAdminFilter.get(filter));
		return value;
	}
	
	/**
	 * 列表查询
	 * @author Moyq5’s Tools
	 * @param admin4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/list")
	@ResponseBody
	public MappingJacksonValue list(@RequestBody Body<SysAdmin4Search, Sorting> admin4s, String filter) {
		log.debug(">>>>list: filter={}", filter);
		List<SysAdmin> list = adminService.list(admin4s.getEntity(), admin4s.getCustom());
		ListResult<SysAdmin> listRst = new ListResult<SysAdmin>();
		listRst.setData(list);
		listRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(listRst);
		value.setFilters(SysAdminFilter.get(filter));
		return value;
	}
	
	/**
	 * 分页查询
	 * @author Moyq5’s Tools
	 * @param admin4s
	 * @param filter
	 * @return
	 */
	@PostMapping("/page")
	@ResponseBody
	public MappingJacksonValue page(@RequestBody Body<SysAdmin4Search, Paging> admin4s, String filter) {
		log.debug(">>>>page: filter={}", filter);
		Page<SysAdmin> page = adminService.page(admin4s.getEntity(), admin4s.getCustom());
		PageResult<SysAdmin> pageRst = new PageResult<SysAdmin>();
		pageRst.setData(page);
		pageRst.setOk();
		MappingJacksonValue value = new MappingJacksonValue(pageRst);
		value.setFilters(SysAdminFilter.get(filter));
		return value;
	}
	
	/**
	 * 添加
	 * @author Moyq5’s Tools
	 * @param form
	 * @return
	 */
	@PutMapping
	@ResponseBody
	public ObjectResult<Long> create(@RequestBody SysAdmin form) {
		log.debug(">>>>add: SysAdmin");
		if (!StringUtils.isEmpty(form.getPwd())) {
			form.setSalt(StringUtils.random(5));
			form.setPwd(MyUtil.encodePwd(form.getPwd() + form.getSalt()));
		}
		SysAdmin admin = adminService.persist(form);
		ObjectResult<Long> result = new ObjectResult<Long>();
		result.setData(admin.getAdminId());
		result.setOk();
		return result;
	}
	
	/**
	 * 更新
	 * @author Moyq5’s Tools
	 * @param form
	 * @return
	 */
	@PatchMapping
	@ResponseBody
	public StatusResult update(@RequestBody SysAdmin form) {
		log.debug(">>>>update: SysAdmin");
		if (!StringUtils.isEmpty(form.getPwd())) {
			form.setSalt(StringUtils.random(5));
			form.setPwd(MyUtil.encodePwd(form.getPwd() + form.getSalt()));
		}
		SysAdmin admin = adminService.one(form.getAdminId());
		EntityUtils.combine(form, admin);
		adminService.merge(admin);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	/**
	 * 删除
	 * @author Moyq5’s Tools
	 * @param adminId
	 * @return
	 */
	@DeleteMapping
	@ResponseBody
	public StatusResult delete(@RequestBody Long adminId) {
		log.debug(">>>>delete: adminId={}", adminId);
		adminService.remove(adminId);
		StatusResult result = new StatusResult();
		result.setOk();
		return result;
	}
	
	
	/**
	 * @author Moyq5
	 * @date 2019年7月15日
	 * @param admin4s
	 * @return
	 */
	@PostMapping("/login")
	@ResponseBody
	public MappingJacksonValue login(@RequestBody SysAdmin4Search admin4s) {
		log.debug(">>>>login: name={}", admin4s.getName());
		String pwd = admin4s.getPwd();
		admin4s.setPwd(null);
		ObjectResult<SysAdmin> result = new ObjectResult<SysAdmin>();
		MappingJacksonValue value = new MappingJacksonValue(result);
		List<SysAdmin> admins = adminService.list(admin4s);
		if (admins.size() != 1) {
			result.setFailMessage("账号或密码有误");
			return value;
		}
		SysAdmin admin = admins.get(0);
		if (!admin.getPwd().equalsIgnoreCase(MyUtil.encodePwd(pwd + admin.getSalt()))) {
			result.setFailMessage("账号或密码有误");
			return value;
		}
		result.setData(admin);
		result.setOk();
		value.setFilters(SysAdminFilter.get());
		return value;
	}
	
}
